<?php
//
// 商铺管理
//
class Controller_Admin_Shop extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'shop';
        parent::__construct($req, $res);

        $this->_list_where = array(
        );

        $this->_sort_extra_columns = array(
            array('changed', DB::expr('curdate()')),
        );

        $this->_shopId = App_Shop_Manager::currentShopId();
        if ($this->_shopId > 0)
        {
            $this->_list_where[] = array('id', '=', $this->_shopId);
            $this->_no_buttons = TRUE;
        }
    }

	//默认首页
    public function action_index ()
    {
        $this->action_list();
    }

	//添加商铺
    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;

            $id = Form_Data::create($this->_form_type, $this->_form_data);
            if ($id > 0)
            {
                App_Shop::initChatUser($id);
            }

            $controller = $this->request->controller(); 
            $directory  = $this->request->directory();
            $directory  = empty($directory) ? '' : '/'.$directory;
            $url = "$directory/$controller/detail/$id";

            $this->request->redirect($url);
        }
        else
        {
            parent::action_create();
        }
    }

	//修改商铺
	//$nid int 商铺ID
    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
            $this->_form_data['shopIcon']   = explode('|', $data['shopIcon']);
            $this->_form_data['shopBanner'] = explode('|', $data['shopBanner']);
        }

        parent::action_update($nid);
    }

	// 商铺详情
	//$nid int 商铺ID
    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 
        $this->_form_data = $data;
        $this->_form_data['shopIcon'] = explode('|', $data['shopIcon']);
        $this->_form_data['shopBanner'] = explode('|', $data['shopBanner']);

        parent::action_detail($nid);
    }

    public function detail_filter_config_data (& $arr_config)
    {
        //
    }

	//重构数据
    private function rebuild_post_data (& $post)
    {
        $post['createrUid'] = user::current_user_id();
        $post['shopIcon']   = implode('|', $post['shopIcon']);
        $post['shopBanner'] = implode('|', $post['shopBanner']);
    }

	//数据格式改造
    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
            //
        }
    }

	//操作菜单
    public function actions_by_list_row (& $row, & $menus)
    {
        //
        if ($this->_shopId > 0)
            array_pop($menus);
    }

}
